package com.yanqu.road.server.cmd.strongholdwar.eliminate;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateDefendPatrons;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateUnion;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manager.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_UPLOAD_UNION_PATRONS, desc = "")
public class CrossUploadEliminateUnionCmd extends CrossUnionCommand {
    private int activityId;
    private List<CrossStrongholdEliminateUnion> unionList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.UploadEliminateUnionDataToCrossMsgReqMsg req = StrongholdWarProto.UploadEliminateUnionDataToCrossMsgReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        for (StrongholdWarProto.UploadEliminateUnionMsg msg : req.getUnionList()) {
            CrossStrongholdEliminateUnion union = new CrossStrongholdEliminateUnion();
            union.setActivtyId(activityId);
            union.setServerId(serverId);
            union.setUnionUid(msg.getUnion().getUnionUid());
            union.setUnionBaseInfo(CrossRankPb.parseUnionBaseInfo(msg.getUnion().getUnionInfo()));
            int unionBaseLv = StrongholdWarMgr.getUnionBaseLv(activityId, msg.getUnion().getUnionUid());
            //union.setStar(msg.getUnion().getLv());
            union.setStar(unionBaseLv);

            List<CrossStrongholdEliminateDefendPatrons> defendPatronsList = new ArrayList<>();
            for (StrongholdWarProto.EliminatePatronsDataMsg dataMsg : msg.getPatronsList()) {
                CrossStrongholdEliminateDefendPatrons patrons = new CrossStrongholdEliminateDefendPatrons();
                patrons.setActivityId(activityId);
                patrons.setServerId(serverId);
                patrons.setUnionUid(union.getUnionUid());
                patrons.setUserBaseInfo(PlayerBasePb.parseToUserBaseInfo(dataMsg.getPlayerData()));
                patrons.setPatrons(JSON.parseObject(dataMsg.getPatrons(), UserPatrons.class));
                patrons.setUserId(patrons.getPatrons().getUserId());
                BigInteger patronsBlood = new BigInteger(dataMsg.getTottalBlood());
                int hpAdd = StrongholdWarMgr.getHpAdd(activityId, union.getUnionUid());
                BigInteger blood = patronsBlood.add(patronsBlood.multiply(BigInteger.valueOf(hpAdd)).divide(new BigInteger("1000")));
                patrons.setCurBlood(new BigInteger(blood.toString()));
                patrons.setTotalBlood(new BigInteger(blood.toString()));
                patrons.setEarnSpeed(patrons.getPatrons().getAbility());
                patrons.setPatronsIndex(dataMsg.getIndex());
                patrons.getPatrons().setAddAbility(new BigInteger(blood.toString()));
                defendPatronsList.add(patrons);
            }
            union.setDefendPatronsList(defendPatronsList);
            unionList.add(union);
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossStrongholdMgr.uploadBase(activityId, groupId, serverId, unionList);
    }

    @Override
    public void sendData() {

    }
}
